		TITLE	ENDSECT - Copyright (c) SLR Systems 1994

		INCLUDE	MACROS
		INCLUDE	IO_STRUC
		INCLUDE	SECTS
		INCLUDE	SECTIONS
		INCLUDE	EXES


		PUBLIC	EXE_OUT_END_OF_SECTION,EO_CV


		.DATA

		EXTERNDEF	TEMP_RECORD:BYTE

		EXTERNDEF	OVL_PANIC_PARAS:DWORD,OVL_DELTA_PARAS:DWORD,RELOC_CHUNK_COUNT:DWORD,SECTION_NUMBER:DWORD
		EXTERNDEF	CURN_SECTION_GINDEX:DWORD,CV_GTYPE_HASH_LOG:DWORD,CV_INDEX_TABLE:DWORD,DONT_PACK:DWORD
		EXTERNDEF	SECTION_PARAS:DWORD,CURN_OUTFILE_GINDEX:DWORD,LAST_CV_MODULE_GINDEX:DWORD
		EXTERNDEF	CODEVIEW_SECTION_GINDEX:DWORD,GSYM_HASH_LOG:DWORD,SSYM_HASH_LOG:DWORD,NEW_REPT_ADDR:DWORD
		EXTERNDEF	FINAL_HIGH_WATER:DWORD,CURN_OUTFILE_BASE_ADDRESS:DWORD,CV_INDEX_COUNT:DWORD
		EXTERNDEF	CURN_SECTION_FILE_ADDRESS:DWORD,EXEHDR_ADDR:DWORD,EXE_OUT_POSITION:DWORD,OVR_INIT_ADDR:DWORD
		EXTERNDEF	VECTOR_SECTION_FILE_ADDRESS:DWORD,BYTES_SO_FAR:DWORD,CV_HEADER:DWORD,CV_HEADER_LOC:DWORD
		EXTERNDEF	FIRST_SECTION_GINDEX:DWORD

		EXTERNDEF	SECTION_GARRAY:STD_PTR_S,OUTFILE_GARRAY:STD_PTR_S,MDB_GARRAY:STD_PTR_S,EXEPACK_STRUCTURE:SEQ_STRUCT
		EXTERNDEF	EXEHEADER:EXE,REAL_RELOC_STRUCT:SEQ_STRUCT

		EXTERNDEF	OUT_FLUSH_SEGMENT:DWORD,CV_SEGTBL:DWORD,CV_PUBLICS_ALL:DWORD,CV_GLOBALS_ALL:DWORD
		EXTERNDEF	CV_TYPES_ALL:DWORD,CV_STATICS_ALL:DWORD,CV_FILES_ALL:DWORD


		.CODE	PASS2_TEXT

		EXTERNDEF	DO_COMPRESS:PROC,FINAL_HIGH_PARA:PROC,MOVE_EAX_TO_FINAL_HIGH_WATER:PROC,FLUSH_OUTFILE_CLOSE:PROC
		EXTERNDEF	FLUSH_EXESTR:PROC,ZERO_EAX_EDX_FINAL:PROC,DO_EXEHEADER:PROC,MOVE_EAX_TO_EDX_FINAL:PROC,ERR_RET:PROC
		EXTERNDEF	FLUSH_PACK:PROC,ZERO_EAX_FINAL_HIGH_WATER:PROC,MOVE_EXEHDR_TO_FINAL:PROC,RELEASE_BLOCK:PROC
		EXTERNDEF	CV_LINNUMS_ALL:PROC,CV_LIBRARIES:PROC,FLUSH_CV_INDEXES:PROC,CV_MODULE_CHANGE:PROC,ERR_ABORT:PROC
		EXTERNDEF	ALLOW_XREF_MAP:PROC,UNUSE_SYMBOLS:PROC,REPORT_UNDEFINEDS_2:PROC,UNUSE_MDB_PARRAY:PROC
		EXTERNDEF	DO_DERIVATIONS:PROC

		EXTERNDEF	COM_PC_ERR:ABS


EXE_OUT_END_OF_SECTION	PROC
		;
		;DEAL WITH END OF A SECTION (SECTION 0 IS MAIN TROUBLE...)
		;
		CALL	OUT_FLUSH_SEGMENT	;FLUSHES BUFFERED EXEPACK STUFF

		MOV	EAX,CURN_SECTION_GINDEX
		MOV	ECX,FIRST_SECTION_GINDEX

		CMP	EAX,ECX
		JZ	L5$

		CMP	CODEVIEW_SECTION_GINDEX,EAX
		JZ	EO_CV

ife	any_overlays
		MOV	AL,0
		JMP	ERR_ABORT
else

if	fg_slrpack
		BITT	SLRPACK_FLAG
		JZ	L1$
		MOV	AX,EXEPACK_STRUCTURE._SEQ_PTR.LW
		MOV	NEW_REPT_ADDR.LW,AX
		MOV	AX,EXEPACK_STRUCTURE._SEQ_PTR.HW
		MOV	NEW_REPT_ADDR.HW,AX
		OR	AX,NEW_REPT_ADDR.LW
		JZ	1$
		CALL	DO_COMPRESS
L1$:
endif
		CALL	FINAL_HIGH_PARA 	;NOW DO RELOCS IF ANY
		;
		;NOW, WRITE # OF PARAS IN IMAGE, AND NUMBER OF RELOC CHUNKS,
		;
		FIXES
		LEA	DI,TEMP_RECORD
		MOV	AX,FINAL_HIGH_WATER.LW
		MOV	DX,FINAL_HIGH_WATER.HW
		ADD	AX,CURN_OUTFILE_BASE_ADDRESS.LW
		ADC	DX,CURN_OUTFILE_BASE_ADDRESS.HW
		SUB	AX,CURN_SECTION_FILE_ADDRESS.LW
		SBB	DX,CURN_SECTION_FILE_ADDRESS.HW
		CALL	SHR_DXAX_4
		OR	AX,AX
.EN CJ
		JZ	2$			;EMPTY SECTION...
.DS CJ
		MOV	SECTION_PARAS,AX	;FOR .MAP
		DEC	AX			;DON'T COUNT THIS PARAGRAPH
		SUB	AX,OVL_PANIC_PARAS	;OR PANIC PARAGRAPHS
		STOSW
		XOR	AX,AX
		XCHG	AX,OVL_DELTA_PARAS
		STOSW				;LOAD OFFSET IN PARAGRAPHS
		XOR	AX,AX
		XCHG	AX,OVL_PANIC_PARAS
		STOSW				;PANIC # OF PARAS
		XOR	AX,AX
		XCHG	AX,RELOC_CHUNK_COUNT
		STOSW
		;
		;WRITE FIRST 8 BYTES OF RELOC STRUCT
		;
		LEA	BX,REAL_RELOC_STRUCT._SEQ_TABLE
		XOR	SI,SI
		MOV	CX,DGROUP:[BX]._SEQ_PTR.LW-_SEQ_TABLE
		JCXZ	19$
		MOV	DS,DGROUP:[BX]
		ASSUME	DS:NOTHING
		SYM_CONV_DS
		;
		;AX IS SMALLER OF 8 AND BYTES LEFT
		;
		MOV	AX,8
		CMP	AX,CX
		JBE	11$
		MOV	AX,CX
11$:
		MOV	CX,4
		REP	MOVSW
		XCHG	AX,CX
19$:
		PUSHM	CX,BX
		FIXDS
		LEA	SI,TEMP_RECORD
		MOV	CX,10H
		CALL	MOVE_EXEHDR_TO_FINAL
		POPM	BX,SI
		OR	SI,SI
		JZ	03$
		SUB	DGROUP:[BX]._SEQ_PTR.LW-_SEQ_TABLE,SI
		;
		;CX IS NUMBER OF BYTES TO WRITE
		;
		XOR	CX,CX
		XCHG	CX,DGROUP:[BX]._SEQ_PTR.LW-_SEQ_TABLE

		ASSUME	DS:NOTHING
01$:
		MOV	DS,DGROUP:[BX]
		SYM_CONV_DS
		;
		;IF CX > PAGE_SIZE, WRITE PAGE_SIZE
		;
		PUSHM	BX,CX
		ADD	CX,SI
		CMP	CX,PAGE_SIZE
		JB	02$
		MOV	CX,PAGE_SIZE
02$:
		SUB	CX,SI
		PUSHM	CX,SI
		;
		;
		;
		PUSHM	CX,DX,AX
		XCHG	AX,CX
		ADD	AX,15
		XOR	DX,DX
		CALL	SHR_DXAX_4
		ADD	SECTION_PARAS,AX
		POPM	AX,DX,CX

		CALL	MOVE_DSSI_TO_FINAL_HIGH_WATER
		POPM	SI,AX,CX,BX
		PUSH	AX
		XOR	AX,AX
		XCHG	AX,DGROUP:[BX]
		CALL	RELEASE_BLOCK
		POP	AX
		ADD	SI,AX
		CMP	SI,PAGE_SIZE
		JNZ	03$
		XOR	SI,SI
		INC	BX
		INC	BX
		SUB	CX,AX
		JNZ	01$
03$:

endif

L2$:
		;
		;NOW, IF DOING OVERLAYS, ROUND OUTPUT TO PARAGRAPH BOUNDARY
		;
if	any_overlays
		BITT	DOING_OVERLAYS
		JZ	21$
		CALL	FINAL_HIGH_PARA
endif

L21$:
if	any_overlays
		LDS	SI,CURN_SECTION
		SYM_CONV_DS
		MOV	AX,SECTION_PARAS
		MOV	[SI]._SECT_FILE_SIZE,AX
endif
		XOR	EAX,EAX
		MOV	ECX,CURN_OUTFILE_GINDEX

		RESS	EXEPACK_SELECTED,AL
		RESS	EXEPACK_BODY,AL
if	fg_slrpack
		RESS	SLRPACK_FLAG,AL
endif
		MOV	DONT_PACK,EAX

		CONVERT	ECX,ECX,OUTFILE_GARRAY
		ASSUME	ECX:PTR OUTFILE_STRUCT

		DEC	[ECX]._OF_SECTIONS	;# OF SECTIONS USING THIS FILE
		JNZ	L25$
		;
		;LAST SECTION TO USE THIS FILE, FLUSH AND CLOSE
		;
		CALL	FLUSH_OUTFILE_CLOSE

		XOR	EAX,EAX
		MOV	CURN_OUTFILE_GINDEX,EAX
		RET
L25$:
;		CALL	FLUSH_OUTFILE
		RET

		ASSUME	ECX:NOTHING
L5$:
		;
		;IF COM, PROBABLY DONE...
		;
		BITT	OUTPUT_COM_SYS
		JNZ	L7$

if	any_overlays
		BITT	DOING_OVERLAYS
		JZ	51$
		CALL	FIX_START_ADDRESS
51$:
endif
		BITT	EXEPACK_SELECTED
		JNZ	L8$
		;
		;ZERO ANY SPACE BETWEEN END OF RELOCS AND START OF EXE
		;
		MOV	EDX,EXEHDR_ADDR
		MOV	EAX,EXE_OUT_POSITION

		SUB	EAX,EDX
		CALL	ZERO_EAX_EDX_FINAL

		CALL	DO_EXEHEADER

		MOV	ECX,EXE._EXE_FIRST_RELOC
L6$:
		PUSHM	EDX,ECX,EAX
		CALL	FLUSH_EXESTR			;WRITE MISCELLANEOUS JUNK
if	any_overlays
		BITT	DOING_OVERLAYS
		JZ	61$
		CALL	FINAL_HIGH_PARA
		;
		;ALLOW SPACE FOR VECTOR AND SECTION STORAGE
		;
		CALL	DO_VECSEC_ZERO
61$:
endif
		POPM	EAX,ECX,EDX
		CALL	MOVE_EAX_TO_EDX_FINAL
		JMP	L2$

L7$:
		BITT	OUTPUT_COM
		JZ	L75$
		;
		;VERIFY START ADDRESS
		;
		CMP	EXEHEADER._EXE_REG_CS,0
		JNZ	L72$
		CMP	EXEHEADER._EXE_REG_IP,100H
		JZ	L75$
L72$:
		MOV	AX,COM_PC_ERR
		CALL	ERR_RET
L75$:
		JMP	L2$

L8$:
		CALL	FLUSH_PACK
		JMP	L6$

EO_CV::
if	fg_cv
		CALL	EO_CV_ROUTINE

		JMP	L21$


else
		RET

endif

EXE_OUT_END_OF_SECTION	ENDP


if	fg_cv

		PUBLIC	EO_CV_ROUTINE


EO_CV_ROUTINE	PROC

		BITT	CODEVIEW_FLAG
		JZ	EO_CV9
L81$:
		CALL	CV_MODULE_CHANGE		;FLUSH ANY REMAINING CODEVIEW INFORMATION

		CMP	LAST_CV_MODULE_GINDEX,0
		JNZ	L81$
if	fg_cvpack
		;
		;RELEASE GSYM_HASH_LOG, CV_GTYPE_HASH_LOG, ETC
		;
		BITT	CVPACK_FLAG
		JZ	L3$
		XOR	EAX,EAX
		XCHG	GSYM_HASH_LOG,EAX
		OR	EAX,EAX
		JZ	L21$
		CALL	RELEASE_BLOCK
L21$:
		XOR	EAX,EAX
		XCHG	SSYM_HASH_LOG,EAX
		OR	EAX,EAX
		JZ	L23$
		CALL	RELEASE_BLOCK
L23$:
		CALL	DO_DERIVATIONS

		XOR	EAX,EAX
		XCHG	CV_GTYPE_HASH_LOG,EAX
		OR	EAX,EAX
		JZ	L22$
		CALL	RELEASE_BLOCK
L22$:



L3$:
endif

		CALL	CV_PUBLICS_ALL			;OUTPUT GLOBALPUB TABLE

		SETT	DOING_DEBUG

;		CALL	ALLOW_XREF_MAP			;I'M DONE WITH SYMBOLS, ALLOW XREF TO RUN

		CALL	REPORT_UNDEFINEDS_2

		CALL	UNUSE_SYMBOLS			;IN FACT, I'M COMPLETELY DONE WITH MAJOR SYMBOL TABLE

		CALL	CV_GLOBALS_ALL			;OUTPUT SSTGLOBALSYM

		CALL	CV_LIBRARIES

		CALL	CV_TYPES_ALL			;OUTPUT SSTGLOBALTYPES

		CALL	CV_STATICS_ALL			;OUTPUT SSTSTATICSYM

		CALL	CV_FILES_ALL			;OUTPUT SSTFILEINDEX

		CALL	UNUSE_MDB_PARRAY

		CALL	CV_SEGTBL			;OUTPUT SEGTBL AND NAMETBL
		;
		;STORE OFFSET TO INDEXES IN CV_HEADER
		;
		MOV	EDX,BYTES_SO_FAR
		MOV	EAX,OFF CV_HEADER

		MOV	CV_HEADER+4,EDX
		MOV	ECX,8

		MOV	EDX,CV_HEADER_LOC
		CALL	MOVE_EAX_TO_EDX_FINAL
		;
		;OUTPUT COUNT OF INDEXES
		;
		MOV	ECX,CV_INDEX_TABLE

		BITT	CV_4_TYPE
		JZ	EO_CV7

		MOV	EAX,12*64K+16		;LENGTH OF THIS STRUCTURE
		MOV	EDX,CV_INDEX_COUNT

		MOV	[ECX],EAX
		MOV	[ECX+4],EDX

		XOR	EAX,EAX

		MOV	[ECX+8],EAX		;LINK TO NEXT DIRECTORY SET
		MOV	[ECX+12],EAX		;UNDEFINED

		JMP	EO_CV8

EO_CV7:
		MOV	EAX,CV_INDEX_COUNT

		MOV	WPTR [ECX],AX
EO_CV8:
		CALL	FLUSH_CV_INDEXES
EO_CV9:
		RET

EO_CV_ROUTINE	ENDP

endif

if	any_overlays

DO_VECSEC_ZERO	PROC	NEAR
		;
		;WRITE ZEROS WHERE WE WILL BE STORING SECTION OFFSETS AND VECTORS (FILLED IN BY RFLUSH)
		;
		MOV	AX,FINAL_HIGH_WATER.LW
		MOV	DX,FINAL_HIGH_WATER.HW
		MOV	VECTOR_SECTION_FILE_ADDRESS.LW,AX
		MOV	VECTOR_SECTION_FILE_ADDRESS.HW,DX
		MOV	BX,AX
		MOV	CX,DX
		MOV	AX,16
		MUL	EXEHEADER._EXE_HDR_SIZE
		SUB	BX,AX
		SBB	CX,DX
		MOV	EXEHEADER._EXE_VECSEC_ADDR.LW,BX
		MOV	EXEHEADER._EXE_VECSEC_ADDR.HW,CX
;		MOV	AX,6			;SECTION #, SEGMENT, OFFSET
;		MUL	VECTOR_COUNT
;		MOV	BX,AX
;		MOV	CX,DX
		XOR	CX,CX
		XOR	BX,BX
		MOV	AX,3			;PARA OFFSET IN FILE
		MUL	SECTION_NUMBER
		ADD	AX,BX
		ADC	DX,CX
		ADD	AX,5			;WORD # OF PARAS IN HEADER, 3-BYTE TIME&DATE
		ADC	DX,0
		XCHG	AX,CX
		XCHG	AX,DX
		CALL	ZERO_AXCX_FINAL_HIGH_WATER
		RET

DO_VECSEC_ZERO	ENDP

FIX_START_ADDRESS	PROC	NEAR
		;
		;FIND SYMBOL REQUESTED BY OVERLAY INIT
		;
		LDS	SI,OVR_INIT_ADDR
		SYM_CONV_DS
		MOV	DX,[SI]._S_FRAME.HW
		MOV	AX,[SI]._S_FRAME.LW
		MOV	BX,[SI]._S_OFFSET.LW
		AND	AL,0F0H
		SUB	BX,AX
		MOV	EXEHEADER._EXE_REG_IP,BX
		CALL	SHR_DXAX_4
		MOV	EXEHEADER._EXE_REG_CS,AX
		RET

FIX_START_ADDRESS	ENDP

endif

		END

